package com.bai.ucenter.service.impl;

import com.bai.base.exceptionHandler.BaiException;
import com.bai.ucenter.entity.Member;
import com.bai.ucenter.entity.vo.MemberVo;
import com.bai.ucenter.mapper.MemberMapper;
import com.bai.ucenter.service.MemberService;
import com.bai.utils.JwtUtils;
import com.bai.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author bai
 * @since 2023-08-14
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //登录返回token
    @Override
    public String login(Member member) {
        if (StringUtils.isEmpty(member.getPassword()) || StringUtils.isEmpty(member.getMobile())) {
            throw new BaiException(2001, "登录失败");
        }
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        wrapper.eq("is_disabled", 0);
        wrapper.eq("mobile", member.getMobile());
        Member one = baseMapper.selectOne(wrapper);
        if (one == null) {
            throw new BaiException(2001, "登录失败");
        }
        if (!MD5.encrypt(member.getPassword()).equals(one.getPassword())) {
            throw new BaiException(2001, "密码错误");
        }
        String jwtToken = JwtUtils.getJwtToken(one.getId(), one.getNickname());

        return jwtToken;
    }


    //注册
    @Override
    public void register(MemberVo memberVo) {
        //获取注册数据
        String code = memberVo.getCode();
        String nickname = memberVo.getNickname();
        String password = memberVo.getPassword();
        String mobile = memberVo.getMobile();
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new BaiException(2001, "注册失败");
        }
        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!redisCode.equals(code)) {
            throw new BaiException(2001, "验证码错误");
        }
        //判断手机号是否重复
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(wrapper);
        if (integer>0){
            throw new BaiException(2001, "该手机号已注册");
        }
        Member member = new Member();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        baseMapper.insert(member);
    }

    @Override
    public Member getOpenIdMenber(String openid) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        Member member = baseMapper.selectOne(wrapper);
        return member;
    }

    /**
     * 查询某一天注册人数
     * @param day
     * @return
     */
    @Override
    public Integer cuntRegister(String day) {
        Integer count= baseMapper.cuntRegister(day);
        return count;
    }
}
